﻿using System;
using System.Globalization;
using System.Threading;
using Hallenberg.Core.Globalization;
using NUnit.Framework;

namespace Hallenberg.Core.Test
{
    [TestFixture]
    public class ChangeThreadCultureTest
    {
        [Test]
        public void ShouldBeAbleToCreateClass()
        {
            var cultureChanger = new ChangeThreadCulture("da-DK");
            Assert.That(cultureChanger, Is.Not.Null);
        }

        [Test]
        public void ShouldChangeThreadCulture()
        {
            var cultureChanger = new ChangeThreadCulture("en-GB");

            Assert.That(Thread.CurrentThread.CurrentCulture.Name, Is.EqualTo("en-GB"));
            Assert.That(Thread.CurrentThread.CurrentUICulture.Name, Is.EqualTo("en-GB"));
        }

        [Test]
        public void ShouldResetThreadCultureOnDispose()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("da-DK");
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("da-DK");

            var cultureChanger = new ChangeThreadCulture("en-GB");

            Assert.That(Thread.CurrentThread.CurrentCulture.Name, Is.EqualTo("en-GB"));
            Assert.That(Thread.CurrentThread.CurrentUICulture.Name, Is.EqualTo("en-GB"));

            cultureChanger.Dispose();

            Assert.That(Thread.CurrentThread.CurrentCulture.Name, Is.EqualTo("da-DK"));
            Assert.That(Thread.CurrentThread.CurrentUICulture.Name, Is.EqualTo("da-DK"));
        }

        [Test]
        public void ShouldNotAcceptNullLanguageName()
        {
            var exception = Assert.Throws<ArgumentNullException>(() => new ChangeThreadCulture(null));

            Assert.That(exception.ParamName, Is.EqualTo("languageName"));
        }

        [Test]
        public void ShouldNotAcceptUnknownLanguageName()
        {
            var exception = Assert.Throws<ArgumentException>(() => new ChangeThreadCulture("Some Unknown Language"));

            Assert.That(exception.ParamName, Is.EqualTo("languageName"));
            Assert.That(exception.Message.StartsWith("The provided language name does not exist"));
        }
    }
}
